<?php 

class Product_model extends CI_Model{
	public function __construct() {
        parent::__construct();
    }
	
	function create($data){
		$data = array(
					'product_name' => $this->input->post('product_name'),
					'description' =>  $this->input->post('description'),
					'price' => $this->input->post('price'),
					'status' => $this->input->post('status'),
					'tags' => $this->input->post('tags'),
					'category' => $this->input->post('category'),
					'option' => $this->input->post('option'),
					'quantity' => $this->input->post('quantity'),
					'weight' => $this->input->post('weight'),
					'height' => $this->input->post('height'),
					'length' => $this->input->post('length'),
					'width' => $this->input->post('width'),
					'date_add' => date ('Y-m-d H:i:s')
				);
		$this->db_front->insert('product',$data);
	}
	
	function getAll($page='', $limit=''){
		if(!empty($limit))
		$this->db_front->limit($limit,$page);
		
		$query = $this->db_front->get('product');
		
		//echo $this->db_front->last_query();
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function update($product_id){
		$data = array(
					'product_name' => $this->input->post('product_name'),
					'description' =>  $this->input->post('description'),
					'price' => $this->input->post('price'),
					'status' => $this->input->post('status'),
					'tags' => $this->input->post('tags'),
					'category' => $this->input->post('category'),
					'option' => $this->input->post('option'),
					'quantity' => $this->input->post('quantity'),
					'weight' => $this->input->post('weight'),
					'height' => $this->input->post('height'),
					'length' => $this->input->post('length'),
					'width' => $this->input->post('width'),
					'date_modified' => date ('Y-m-d H:i:s')
				);
        $this->db_front->where('product_id', $product_id);
        $this->db_front->update('product', $data);
    }
	
	function select($product_id){
        return $this->db_front->get_where('product', array('product_id'=>$product_id))->row();
    }
	
	function delete($product_id){
        $this->db_front->delete('product', array('product_id'=>$product_id));
    }
	
	function category(){
		return $this->db_front->query('select category_id, category_name from  product_category order by category_name')-> result();
	}
	
	function weight_class(){
		return $this->db_front->query('select * from  product_weight_class order by title')-> result();
	}
	
	function length_class(){
		return $this->db_front->query('select * from  product_length_class order by title')-> result();
	}
	
	function get_product_by_id($product_id){
		$this->db_front->where('product_id', $product_id);
		$this->db_front->from('product');
		$query = $this->db_front->get();
		return $query->row_array();		
	}
	
	function get_option($product_id){
		$this->db_front->where('product_id', $product_id);
		$query = $this->db_front->get('product_option');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function get_shipping_by_product($product_id){
		$this->db_front->where('product_id', $product_id);
		$query = $this->db_front->get('product_shipping');
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
		
	}
}
?>